import sys,os,math,time
import matplotlib.pyplot as plt
from numpy import *

xdim = [(-0.1,0.3), (0.5,0.7), (-0.5,0.2),(-0.7,0.3),(0.7,0.1),(0,0.5)]
ddim = [1,-1,1,1,-1,1]

def sigmoid(x):
    return 1/(1+exp(-x))

def hebbian(w,x,d):
    x1 = [1, x[0], x[1]]
    net = sum([ww*xx for ww, xx in zip(w, x1)])
    o = sigmoid(net)
    w1 = [ww+o*xx for ww, xx in zip(w, x1)]
    return w1

def perceptron(w,x,d):
    x1 = [1,x[0],x[1]]
    net = sum([ww*xx for ww, xx in zip(w, x1)])
    o = 1 if net >= 0 else -1
    w1 = [ww+(d-o)*xx for ww, xx in zip(w, x1)]
    return w1

def delta(w,x,d):
    x1 = [1,x[0],x[1]]
    net = sum([ww*xx for ww,xx in zip(w, x1)])
    o = sigmoid(net)
    o1 = o*(1-o)
    w1 = [ww+(d-o)*o1*xx for ww,xx in zip(w,x1)]
    return w1

def widrawhoff(w,x,d):
    x1 = [1,x[0],x[1]]
    _sum = sum([ww*xx for ww,xx in zip(w, x1)])
    w1 = [ww+(d-_sum)*xx for ww,xx in zip(w,x1)]
    return w1

def correlation(w,x,d):
    x1 = [1, x[0], x[1]]
    w1 = [ww+d*xx for ww,xx in zip(w,x1)]
    return w1


wb1 = [0,0,0]                        # [b, w1, w2]
wb2 = [0,0,0]
wb3 = [0,0,0]
wb4 = [0,0,0]
wb5 = [0,0,0]

marker='o'
color ='blue'
for x, d in zip(xdim, ddim):
    wb1 = hebbian(wb1, x, d)
    wb2 = perceptron(wb2, x, d)
    wb3 = delta(wb3, x, d)
    wb4 = widrawhoff(wb4, x, d)
    wb5 = correlation(wb5, x, d)

wb = [wb1, wb2, wb3, wb4, wb5]
func_name = ["hebbian", "perceptron", "delta", "widrawhoff", "correlation"]
colors = ['green', 'orange', 'red', "yellow", "blue"]
for i,cl,func in zip(wb, colors, func_name):
    plt.scatter(i[1], i[2], marker=marker, c=cl)
    plt.text(i[1], i[2], func + '(%1.3f,%1.3f)' % (i[1], i[2]))

plt.xlabel("w1")
plt.ylabel("w2")
plt.grid(True)
plt.tight_layout()
plt.show()




